﻿using System.Collections.Generic;
using System.Windows;

namespace Kugar.Core.SL4.Control
{
    public class ComboBoxBindClass<T>:DependencyObject
    {
        public static DependencyProperty SelectItemProperty = DependencyProperty.Register("SelectItem", typeof (T),
                                                                                           typeof (ComboBoxBindClass<T>),
                                                                                           new PropertyMetadata(default(T)));


        public static DependencyProperty ItemsProperty = DependencyProperty.Register("Items", typeof(IEnumerable<ComboBoxItemSource<T>>),
                                                                                   typeof(ComboBoxBindClass<T>),
                                                                                   new PropertyMetadata(null));

        public T SelectItem
        {
            get { return (T)GetValue(SelectItemProperty); }
            set { SetValue(SelectItemProperty, value); }    
        }

        public IEnumerable<ComboBoxItemSource<T>> Items
        {
            get { return (IEnumerable<ComboBoxItemSource<T>>)GetValue(ItemsProperty); }
            set { SetValue(ItemsProperty, value); }
        }

    }


    public class ComboBoxItemSource<T>
    {
        public ComboBoxItemSource()
        {}

        public ComboBoxItemSource(string text, T value)
        {
            Text = text;
            Value = value;
        }

        public string Text { set; get; }
        public T Value { set; get; }



        public static implicit operator T(ComboBoxItemSource<T> value)
        {
            return value.Value;
        }


        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if (obj.GetType() != typeof (ComboBoxItemSource<T>)) return false;
            return Equals((ComboBoxItemSource<T>) obj);
        }

        public bool Equals(ComboBoxItemSource<T> other)
        {
            if (ReferenceEquals(null, other)) return false;
            if (ReferenceEquals(this, other)) return true;
            return Equals(other.Value, Value);
        }

        public override int GetHashCode()
        {
            return Value.GetHashCode();

            //unchecked
            //{
            //    return ((Text != null ? Text.GetHashCode() : 0)*397) ^ Value.GetHashCode();
            //}
        }
    }


}
